const _ = require('lodash');

function getArea(r) {
  console.log(r);
  return Math.PI * r * r;
}

// let getAreaWithMemoize = _.memoize(getArea);
// console.log(getAreaWithMemoize(3));
// console.log(getAreaWithMemoize(3));
// console.log(getAreaWithMemoize(3));
// console.log(getAreaWithMemoize(4));

// 模拟memoize
function memoize(fn) {
  let cache = {};
  return function () {
    let key = JSON.stringify(arguments);
    console.log(key);
    cache[key] = cache[key] || fn.apply(fn, arguments);
    return cache[key];
  }
}

let getAreaWithMemoize = memoize(getArea);
console.log(getAreaWithMemoize(3, 5));
console.log(getAreaWithMemoize(3));
console.log(getAreaWithMemoize(3));
console.log(getAreaWithMemoize(4));